import { defineStore } from 'pinia'

export type RouterState = {
    KeepAliveRouters: any
    defaultRouter: string
    backRouter: string
}

// 定义store
export const routerStore = defineStore('routerStore', {
    state: (): RouterState => {
        return {
            KeepAliveRouters: [],
            defaultRouter: '/dashboard',
            backRouter: '/superback'
        }
    },
    actions: {
        setKeepAliveRouters(name: string, keep_alive: boolean) {
            if (keep_alive == true) {
                if (
                    !this.KeepAliveRouters.some((item: string) => item == name)
                ) {
                    this.KeepAliveRouters.push(name)
                }
            }
        },
        delKeepAliveRouters(name: string) {
            // console.log('delKeepAliveRouters=' + name)
            const index = this.KeepAliveRouters.indexOf(name, 0)
            if (index > -1) {
                this.KeepAliveRouters.splice(index, 1)
            }
        }
    },
    getters: {
        getKeepAliveRouters(state) {
            return state.KeepAliveRouters
        },
        getDefaultRouter(state) {
            return state.defaultRouter
        },
        getBackRouter(state) {
            return state.backRouter
        }
    }
})
